package com.happyfamily.springboot.crowdsourcingplatform.controller.sun;

import com.gargoylesoftware.htmlunit.html.HtmlTable;
import com.google.gson.Gson;
import com.happyfamily.springboot.crowdsourcingplatform.annotation.OperationLogDetail;
import com.happyfamily.springboot.crowdsourcingplatform.dto.LoginBackUser;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationType;
import com.happyfamily.springboot.crowdsourcingplatform.enums.OperationUnit;
import com.happyfamily.springboot.crowdsourcingplatform.model.SysUser;
import com.happyfamily.springboot.crowdsourcingplatform.model.TableModel;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.NewService;
import com.happyfamily.springboot.crowdsourcingplatform.service.sun.SysUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextImpl;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/api-front/news/")
public class NewController
{
	@Autowired
	private NewService newServiceImpl;

	@Autowired
	private SysUserService userServiceImpl;

	@RequestMapping("getList")
	@ResponseBody
	@ApiOperation(value = "服务商查询所有自己的消息", notes = "服务商查询所有自己的消息", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商查询所有自己的消息", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public String getList(String limit, String page, HttpServletRequest request)
	{
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		TableModel tableModel=new TableModel();
		tableModel.setMsg("");
		tableModel.setCode(0);
		tableModel.setCount(newServiceImpl.getSum(account));
		tableModel.setData(newServiceImpl.getList(account,Integer.valueOf(limit),Integer.valueOf(page)));
		Gson g=new Gson();
		return g.toJson(tableModel);
	}

	@RequestMapping("deleteNews")
	@ResponseBody
	@ApiOperation(value = "服务商删除某条自己的消息", notes = "服务商删除某条自己的消息", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商删除某条自己的消息", level = 4, operationUnit = OperationUnit.USER, operationType = OperationType.DELETE)
	public String deleteNews(String nid)
	{
		String str="no";
		boolean flag=newServiceImpl.deleteNews(Long.valueOf(nid));
		if(flag)
		{
			str="yes";
		}
		return str;
	}

	@RequestMapping("updateCount")
	@ResponseBody
	@ApiOperation(value = "服务商修改未读消息为0", notes = "服务商修改未读消息为0", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商修改未读消息为0", level = 2, operationUnit = OperationUnit.USER, operationType = OperationType.UPDATE)
	public String updateCount(HttpServletRequest request)
	{
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		userServiceImpl.updateCount(account,0);
		return "";
	}

	@RequestMapping("getCount")
	@ResponseBody
	@ApiOperation(value = "服务商查询自己的未读消息", notes = "服务商查询自己的未读消息", httpMethod = "POST", response = String.class)
	@OperationLogDetail(detail = "服务商查询自己的未读消息", level = 1, operationUnit = OperationUnit.USER, operationType = OperationType.SELECT)
	public String getCount(HttpServletRequest request)
	{
		SecurityContextImpl securityContextImpl = (SecurityContextImpl) request.getSession().getAttribute("SPRING_SECURITY_CONTEXT");
		String account = securityContextImpl.getAuthentication().getName();
		SysUser sysUser=userServiceImpl.getUser(account);

		return sysUser.getNewcount()+"";
	}
}